/*
 * Javascript Cairngorm Bridge
 *
 * @version 0.1
 * 
 * http://squarefactor.com
 *
 * Licensed under the MIT license:
 *   http://www.opensource.org/licenses/mit-license.php
 *   
 * Usage:
 * 	See the README.txt
 *   
 * Dependencies:
 *   Cairngorm 2.2.1 - Be sure to add this to your Flex app.
 *   FABridge - Part of the Flex SDK. At the time of writing (SDK 3.2) it was located in /frameworks/javascript/fabridge.
 *              You will need to include the src folder in your Flex Project path and FABridge.js in your html.
 *              The reason for the dependencies is the license for the Flex Ajax Bridge code says I can not 
 *              redistribute it. In you Flex project just add <bridge:FABridge /> in your main mxml.
 *              For more info on Flex Ajax Bridge see: http://livedocs.adobe.com/flex/3/html/help.html?content=ajaxbridge_1.html
 *   
 * Note:
 *   Be sure that if you have only used an event/value object/etc from javascript that you have forced
 *   it to be compiled into the swf by at least referencing it somewhere.
 * 
 */

/**
 * 
 * @param {String} eventPackagePath The package path for your cairngorm events. Ex: "com.domain.event"
 * @param {String} valueObjectPackagePath The package path for your value objects. Ex: "com.domain.vo"
 * @param {Function} readyCallback The function to call when everything is ready to use.
 * @param {String} bridgeName The name of the bridge if it was specified as a flash var for your swf. This is only
 *                            really used if you have multiple swfs on the same page using the Flex Ajax Bridge.
 */
function JavascriptCairngormBridge(eventPackagePath, valueObjectPackagePath, readyCallback, bridgeName){
  var thisRef = this;
  
  this.eventPackagePath = eventPackagePath;
  this.valueObjectPackagePath = valueObjectPackagePath;
  this.bridgeName = !bridgeName || bridgeName.length < 1 ? "flash" : bridgeName;
  
  if(FABridge[this.bridgeName]){
    this.app = FABridge[this.bridgeName].root();
    readyCallback.call(this);
  }else{
    FABridge.addInitializationCallback(this.bridgeName, 
      function(){
        thisRef.app = FABridge[thisRef.bridgeName].root();
        readyCallback.call(thisRef);
      }
    );
  }
}

JavascriptCairngormBridge.prototype = {
  
  /**
   * Creates and dispatches the given event class.
   * 
   * @param {String} className The name of the event class to dispatch.
   * @param {Object} optional_argsMap An object map of property names and values to set on the event before dispatching.
   */
  dispatchEvent: function(className, optional_argsMap){
    try {
      var event = FABridge[this.bridgeName].create(this.eventPackagePath + "." + className);
      
      if(optional_argsMap instanceof Object){
        for(var p in optional_argsMap){
          event.set(p, optional_argsMap[p]);
        }
      }
      
      event.dispatch();
    }catch(e){
      // This is likely because your cairngorm event required arguments.
      // Make them all optional and allow them to be set.
    }
  },
  
  /**
   * Creates and returns the given value object class.
   * 
   * @param {String} className The name of the value object class to create.
   * @param {Object} optional_argsMap An object map of property names and values to set on the event before returning.
   */
  getVO: function(className, optional_argsMap){
    try {
      var vo = FABridge[this.bridgeName].create(this.valueObjectPackagePath + "." + className);
      
      if(optional_argsMap instanceof Object){
        for(var p in optional_argsMap){
          vo.set(p, optional_argsMap[p]);
        }
      }
      
      return vo;
    }catch(e){
      // This is likely because your value object required arguments.
      // Make them all optional and allow them to be set.
    }
    
    return null;
  }
  
}
